#!/bin/bash
BASH_PATH=$(cd "$(dirname "$0")";pwd)
common_info_path=/opt/daas-info/common-info.conf

# 加载common_info配置文件
source $common_info_path

# 定义前台安装目录
frontend_src_path=${BASH_PATH}/.. 
echo $frontend_src_path
frontend_dest_path=${frontend_install_path}

# 定义日志文件路径
deploy_front_log=${deploy_log_path}/user-auth-front.log

# 创建安装目录(前端)
mkdir -p ${deploy_log_path}

scp_file(){
	ssh root@$local_ip "mkdir -p ${frontend_dest_path}"
	scp_upload $frontend_src_path/user-auth-front root $local_ip $frontend_dest_path
}

echo_to_log(){
    file=$1
    message=$2
    if [[ ! -f $file ]]; then
        dir_name=$(dirname $file)
        mkdir -p $dir_name
        touch $file
    fi
    time=$(date "+%Y-%m-%d %H:%M:%S")
    echo -e "\n------------------" >> $file
    echo $time >> $file
    echo $message >> $file
    echo -e "------------------\n" >> $file

}
# 上传文件到其他主机
# 前提以及完成copy_pub_key复制公钥到其他主机的工作
# 参数说明：<当前主机路径> <其他主机ip用户名> <其他主机ip> <ip对应的路径>
scp_upload() {
  #获取IP
   local FILE_PATH=$1
   local USER_NAME=$2
   local USER_IP=$3
   local USER_DIR_PATH=$4

   scp -r $FILE_PATH $USER_NAME@$USER_IP:$USER_DIR_PATH
   if [ $? -eq 0 ]; then
       echo "Upload $FILE_PATH to $USER_NAME@$USER_IP successfully."
   else
       echo "Upload $FILE_PATH to $USER_NAME@$USER_IP failed."
   fi
}

# 将前后端放到对应位置
scp_file

front_dist_path=${frontend_dest_path}user-auth-front/dist
if ssh root@$local_ip test -e $front_dist_path;then
echo "user-auth-front_v3.0.2 successful"
echo_to_log $deploy_front_log "user-auth-front_v3.0.2 successful"
else
echo "传输user-auth-front到指定目录失败"
echo_to_log $deploy_front_log "传输user-auth-front到指定目录失败"
fi

